#include <iostream>
#include <ctime>
#include <cstdlib>
#include <cstring>
#include <chrono>
#include <memory>
#include "datetime.h"

namespace wb {

//using sys_clock = std::chrono::system_clock;

const Date Date::datetime()
{
    return Date();
}

const Date Date::today() const
{
    return Date(m_dayspoint);
}

const Date Date::tomorrow() const
{
    return Date(m_timepoint + days_dur(1));
}

const Date Date::secondsLater(int64_t secs) const
{
    return Date(m_timepoint + seconds_dur(secs));
}

std::shared_ptr<struct tm> Date::tmStruct() const
{
    time_t tt = sys_clock::to_time_t(m_timepoint);
    std::shared_ptr<struct tm> ret(new struct tm);
    memcpy(ret.get(), localtime(&tt), sizeof(struct tm));
    return ret;
}

std::string Date::toString() const
{
    const char* fmt = "%F %T";
    char buf[100] = {0};
    std::time_t tt = sys_clock::to_time_t(m_timepoint);
    std::strftime(buf, sizeof(buf), fmt, std::localtime(&tt));
    return std::string(buf);
}

std::string Date::toString(const char* fmtstr) const
{
    if (fmtstr == nullptr)
    {
        return timeStamp();
    }
    char buf[100] = {0};
    std::time_t tt = sys_clock::to_time_t(m_timepoint);
    std::strftime(buf, sizeof(buf), fmtstr, std::localtime(&tt));

    return std::string(buf);
}

std::string Date::timeStamp() const
{
    return std::to_string(
        std::chrono::time_point_cast<seconds_dur>(
            m_timepoint
        ).time_since_epoch().count()
    );
}



















}